package com.analyst.service.impl;



import com.analyst.jdbc.JdbcExample;
import com.analyst.mapper.SqlJobMapper;
import com.analyst.service.ILogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.analyst.entity.Job;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class LogService extends ServiceImpl<SqlJobMapper, Job> implements ILogService {


    @Resource
    private SqlJobMapper mapper;


    static boolean  isSqlString=true;
    @Override
    public  void parserAndInsert(){

            mapper.dropTable();
            mapper.createTable();

        String fileName ="src/main/resources/SQL.txt";
        List<Job> jobs = readFile(fileName);
        for (Job job : jobs) {
            mapper.insert(job);
        }





//        JdbcExample.insert(jobs);

    }


    public void createJobs(){

    }


    public static void main(String[] args) {
        String fileName ="src/main/resources/SQL.txt";
        List<Job> jobs = readFile(fileName);
        System.out.println(jobs);
        JdbcExample.insert(jobs);
//        System.out.println(jobs.get(0).getDuration());

    }
    public static Date toDate(String dateString){
        dateString="2"+dateString;
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss,SSS");
        try {
            return format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static List<Job> readFile(String fileName){
        ArrayList<Job> jobs = new ArrayList<>();
        try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {
            String line;
            Job job = new Job();
            StringBuilder stringBuilder=new StringBuilder();

            while ((line = br.readLine()) != null) {
                if (line.isEmpty()){
                    continue;
                }
                Date date = getDate(line);
                if ( date==null) {
                    if (isSqlString) {
                        //
                        //添加进sql字段 继续添加
                        if (stringBuilder.length()==0)
                            stringBuilder.append(line);
                        else
                            stringBuilder.append("\n").append(line);

                    } else {
                        //日志结束
                        //加入list
                        jobs.add(new Job(stringBuilder.toString(),job.getBeginTime(),job.getEndTime()));
                        job=new Job();
                        stringBuilder=new StringBuilder(line);

                        isSqlString=true;

                    }
                } else  {//日志
                    if (isSqlString) {
                        //设置beginTime
                        job.setBeginTime(date);
                        isSqlString=false;
                    } else {
                        //设置endtime
                        job.setEndTime(date);
                    }
                }
            }
            job.setSqlString(stringBuilder.toString());
            jobs.add(job);
        }
        catch (IOException e) {
            e.printStackTrace();
            return null ;
        }
        return jobs;
    }

    public static Date getDate(String line){
        Pattern pattern = Pattern.compile("\\d{3}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}");  // 创建正则表达式模式以匹配日期时间格式
        Matcher matcher = pattern.matcher(line);  // 在文件内容中寻找匹配日期时间格式的字符串


        if (matcher.find()) {  // 循环查找匹配日期时间格式的字符串
            String dateTimeString = matcher.group().substring(1);  // 删除年份前的0
            Date date =toDate(dateTimeString) ; // 将字符串转换为日期时间对象
            return date;

        }
        return null;
    }
}
